package lib.control;

import lib.JBierSubAdapter;
import lib.JTVRageAdapter;
import lib.api.bierdopje.model.JBierEpisode;
import lib.api.bierdopje.model.JBierShow;
import lib.api.tvrage.model.TVRageEpisode;
import lib.api.tvrage.model.TVRageShowInfo;
import lib.exception.VideoControlException;
import lib.logging.Logger;
import lib.model.EpisodeFile;
import lib.model.Subtitle;
import settings.model.MappingBierdopjeScene;
import settings.model.Settings;
import settings.model.SettingsProcessEpisodeSource;

import java.util.ArrayList;
import java.util.List;

public class EpisodeFileControl extends VideoFileControl {

	private final JBierSubAdapter jbs;
	private final JTVRageAdapter tvra;

	public EpisodeFileControl(EpisodeFile episodeFile, Settings settings) {
		super(episodeFile, settings);
		jbs = new JBierSubAdapter();
		tvra = new JTVRageAdapter();
	}

	public EpisodeFile processBierDopje(EpisodeFile episodeFile,
			List<MappingBierdopjeScene> dict) throws VideoControlException {

		JBierShow bshow = jbs.searchShow(episodeFile, dict);
		if (bshow != null) {
			episodeFile.updateBierShowInfo(bshow);
			episodeFile.setOriginalShowName(getOriginalShowName(
					bshow.getShowid(), dict));
			JBierEpisode bepisode = jbs.searchEpisode(episodeFile, bshow);
			if (bepisode != null) {
				episodeFile.updateBierEpisodeInfo(bepisode);
			} else {
				throw new VideoControlException("Season "
						+ episodeFile.getSeason() + " Episode "
						+ episodeFile.getEpisodeNumbers().toString()
						+ "not found, check file", videoFile);
			}
		} else {
			throw new VideoControlException("Show not found, check file",
					videoFile);
		}
		return episodeFile;
	}

	public EpisodeFile processTVRage(EpisodeFile episodeFile) {
		TVRageShowInfo tvrShowInfo = tvra.searchShow(episodeFile);
		TVRageEpisode tvrEpisode = tvra.getEpisodeInfo(tvrShowInfo.getShowID(), episodeFile.getSeason(), episodeFile.getEpisodeNumbers().get(0));
		
		episodeFile.updateTVRageEpisodeInfo(tvrEpisode);

		return episodeFile;
	}

	public EpisodeFile process(List<MappingBierdopjeScene> dict)
			throws VideoControlException {
		Logger.instance.trace("EpisodeFileControl", "process", "");

		EpisodeFile episodeFile = (EpisodeFile) videoFile;
		// return episodeFile;
		if (episodeFile.getShow().equals("")) {
			throw new VideoControlException(
					"Unable to extract episode details, check file", videoFile);
		} else {
			Logger.instance.debug("Showname: " + episodeFile.getShow());
			Logger.instance.debug("Season: " + episodeFile.getSeason());
			Logger.instance
					.debug("Episode: " + episodeFile.getEpisodeNumbers());

			if (settings.getProcessEpisodeSource().equals(
					SettingsProcessEpisodeSource.TVRAGE)) {
				return processTVRage(episodeFile);
			}
			return processBierDopje(episodeFile, dict);
		}
	}

	public String getOriginalShowName(int bierdopjeId,
			List<MappingBierdopjeScene> dict) {
		for (MappingBierdopjeScene aDict : dict) {
			if (bierdopjeId == aDict.getBierdopjeId()) {
				Logger.instance.debug("Found Original(Scene) Name in mapping: "
						+ aDict.getSceneName());
				return aDict.getSceneName();
			}
		}
		return "";
	}

	public EpisodeFile processWithSubtitles(List<MappingBierdopjeScene> dict,
			String languageCode) throws VideoControlException {
		Logger.instance.trace("EpisodeFileControl", "processWithSubtitles", "");
		EpisodeFile episode = process(dict);
		if (episode != null) {
			List<Subtitle> listFoundSubtitles = new ArrayList<Subtitle>();
			listFoundSubtitles.addAll(sc.getSubtitles(episode, languageCode));
			episode.setMatchingSubs(listFoundSubtitles);
		}
		return episode;
	}
	
	

}
